package cn.sirun.com.fca.account.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import java.util.Map;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountCommonConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.RequestParamConst;
import cn.sirun.com.fca.account.listener.OnBackPressedListener;
import cn.sirun.com.fca.account.model.bind.RecognizePicResponse;
import cn.sirun.com.fca.account.uitl.AccountViewUtils;
import cn.sirun.com.fca.account.uitl.StorageUtils;


/**
 * @author vinsonhuang
 * @date 2018/5/11
 */
public class ConfirmInfoFragment extends Fragment implements OnBackPressedListener {

    private static final String TAG = ConfirmInfoFragment.class.getSimpleName();

    private static final int VEHICLE_IDENTITY_NUMBER_LENGTH = 17;

    private String mUrl = "";

    private String mOwnerOrName = "";

    private String mVehicleIdentityNumber = "";

    private String mEngineIdOrIdentity = "";

    private TextView mTitleTV;

    private TextView mOwnerOrNameTV;

    private TextView mVehicleIdentityNumberTV;

    private TextView mEngineIdOrIdentityTV;

    private LineEditText mOwnerOrNameET;

    private LineEditText mVehicleIdentityNumberET;

    private LineEditText mEngineIdOrIdentityET;

    private LinearLayout mVehicleIdentityNumberLL;

    private View mDivisionView;

    private Button mConfirmInfoBtn;

    private OnConfirmListener mConfirmListener;

    private boolean isReviewAuthInfo = false;

    private RequestParamConst.PictureTypeEnum mPictureType
            = RequestParamConst.PictureTypeEnum.IDENTITY_CARD;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.confirm_info_fragment, container, false);

        mTitleTV = view.findViewById(R.id.tv_confirm_info_title);
        mOwnerOrNameTV = view.findViewById(R.id.tv_owner_or_name);
        mVehicleIdentityNumberTV = view.findViewById(R.id.tv_vin);
        mEngineIdOrIdentityTV = view.findViewById(R.id.tv_engine_id_or_identity);

        mOwnerOrNameET = view.findViewById(R.id.et_owner_or_name);
        mVehicleIdentityNumberET = view.findViewById(R.id.et_vin);
        mEngineIdOrIdentityET = view.findViewById(R.id.et_engine_id_or_identity);
        mConfirmInfoBtn = view.findViewById(R.id.btn_confirm_info);

        mVehicleIdentityNumberLL = view.findViewById(R.id.ll_vin);
        mDivisionView = view.findViewById(R.id.view_division);

        mVehicleIdentityNumberET.setInputLength(VEHICLE_IDENTITY_NUMBER_LENGTH);
        AccountViewUtils.adjustTitleMargin(mTitleTV);

        mOwnerOrNameET.requestFocus();
        initListener();
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isReviewAuthInfo = getArguments().getBoolean(AccountIntentConst.IS_REVIEW_AUTH_INFO);
        updateView();
    }

    @Override
    public boolean onBackPressed() {
        StorageUtils.removeAuthInfo(mPictureType);
        mConfirmListener.onBackPressed();
        return true;
    }

    public void updateView() {
        if (RequestParamConst.PictureTypeEnum.IDENTITY_CARD == mPictureType) {
            mVehicleIdentityNumberLL.setVisibility(View.GONE);
            mDivisionView.setVisibility(View.GONE);

            mTitleTV.setText("123213");
            mOwnerOrNameTV.setText("3213");
            mEngineIdOrIdentityTV.setText("3213");
        } else {
            mVehicleIdentityNumberLL.setVisibility(View.VISIBLE);
            mDivisionView.setVisibility(View.VISIBLE);

            mTitleTV.setText("3213");
            mOwnerOrNameTV.setText("3213");
            mVehicleIdentityNumberTV.setText("3213");
            mEngineIdOrIdentityTV.setText("3213");
        }

        if (isReviewAuthInfo) {
            Map<String, String> values = StorageUtils.getAuthInfo(mPictureType);
            if (null != values) {
                mUrl = values.get(AccountCommonConst.AUTH_URL);
                mOwnerOrName = values.get(AccountCommonConst.OWNER_OR_NAME);
                mEngineIdOrIdentity = values.get(AccountCommonConst.ENGINE_ID_OR_IDENTITY);
                mVehicleIdentityNumber = values.get(AccountCommonConst.VEHICLE_IDENTITY_NUMBER);
            }
            mOwnerOrNameET.setEnabled(false);
            mVehicleIdentityNumberET.setEnabled(false);
            mEngineIdOrIdentityET.setEnabled(false);
            mConfirmInfoBtn.setText("3213");
        } else {
            mConfirmInfoBtn.setText("3213");
        }

        mOwnerOrNameET.setText(mOwnerOrName);
        mEngineIdOrIdentityET.setText(mEngineIdOrIdentity);
        if (RequestParamConst.PictureTypeEnum.VEHICLE_LICENSE == mPictureType) {
            mVehicleIdentityNumberET.setText(mVehicleIdentityNumber);
        }
    }

    public void setInitData(BaseResponse<RecognizePicResponse> response,
                            RequestParamConst.PictureTypeEnum pictureType) {
        mPictureType = pictureType;
        mOwnerOrName = "";
        mVehicleIdentityNumber = "";
        mEngineIdOrIdentity = "";
        if (null == response || null == response.getData()) {
            return;
        }
        RecognizePicResponse picResponse = response.getData();
        if (RequestParamConst.PictureTypeEnum.VEHICLE_LICENSE == mPictureType) {
            mOwnerOrName = picResponse.getOwner() != null ? picResponse.getOwner() : "";
            mVehicleIdentityNumber =
                    picResponse.getCarNumber() != null ? picResponse.getCarNumber() : "";
            mEngineIdOrIdentity =
                    picResponse.getEngineId() != null ? picResponse.getEngineId() : "";
        } else {
            mOwnerOrName = picResponse.getName() != null ? picResponse.getName() : "";
            mEngineIdOrIdentity = picResponse.getId() != null ? picResponse.getId() : "";
        }
    }

    public void setConfirmListener(OnConfirmListener confirmListener) {
        mConfirmListener = confirmListener;
    }

    public interface OnConfirmListener {
        void onConfirmCallback(String url, String ownerOrName,
                               String vehicleIdentityNumber,
                               String engineIdOrIdentity);

        void onBackPressed();
    }

    private void initListener() {
        mConfirmInfoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isReviewAuthInfo) {
                    mOwnerOrNameET.setEnabled(true);
                    mVehicleIdentityNumberET.setEnabled(true);
                    mEngineIdOrIdentityET.setEnabled(true);
                    mOwnerOrNameET.setText("");
                    mVehicleIdentityNumberET.setText("");
                    mEngineIdOrIdentityET.setText("");
                    mConfirmInfoBtn.setText("3213");
                    mConfirmInfoBtn.setEnabled(false);
                    isReviewAuthInfo = false;
                    return;
                }
                uploadAuthInfo();
            }
        });

        mOwnerOrNameET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                updateConfirmBtnState();
            }
        });

        mVehicleIdentityNumberET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                updateConfirmBtnState();
            }
        });

        mEngineIdOrIdentityET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                updateConfirmBtnState();
            }
        });
    }

    private void updateConfirmBtnState() {
        int nameLength = mOwnerOrNameET.length();
        int vehicleLength = mVehicleIdentityNumberET.length();
        int idLength = mEngineIdOrIdentityET.length();
        if (nameLength > 0 && idLength > 0) {
            if (RequestParamConst.PictureTypeEnum.IDENTITY_CARD == mPictureType) {
                mConfirmInfoBtn.setEnabled(true);
                return;
            } else {
                if (vehicleLength > 0) {
                    mConfirmInfoBtn.setEnabled(true);
                    return;
                }
            }
        }
        mConfirmInfoBtn.setEnabled(false);
    }

    private void uploadAuthInfo() {
        mOwnerOrName = mOwnerOrNameET.getText().toString();
        mVehicleIdentityNumber = mVehicleIdentityNumberET.getText().toString();
        mEngineIdOrIdentity = mEngineIdOrIdentityET.getText().toString();
        if (RequestParamConst.PictureTypeEnum.VEHICLE_LICENSE == mPictureType) {
            if (TextUtils.isEmpty(mOwnerOrName)
                    || TextUtils.isEmpty(mVehicleIdentityNumber)
                    || TextUtils.isEmpty(mEngineIdOrIdentity)) {
                return;
            }
        } else {
            if (TextUtils.isEmpty(mOwnerOrName)
                    || TextUtils.isEmpty(mEngineIdOrIdentity)) {
                return;
            }
        }

        if (null != mConfirmListener) {
            StorageUtils.removeAuthInfo(mPictureType);
            mConfirmListener.onConfirmCallback(mUrl, mOwnerOrName,
                    mVehicleIdentityNumber, mEngineIdOrIdentity);
        }
    }
}
